﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Controls_UC_Dathang : System.Web.UI.UserControl
{
    BUS_Province province = new BUS_Province();
    BUS_Customers customer = new BUS_Customers();
    BUS_PayMethod paymethod = new BUS_PayMethod();
    BUS_Product product = new BUS_Product();
    static DataTable tbGioHang = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        Page.Title = "Thông tin đặt hàng";
        if (Session["UserName"] == null)
        {
            Response.Redirect("~/Trangchu.aspx");
        }
        else
        {

            if (!IsPostBack)
            {
                LoadData();
            }
        }
        
    }
    public void LoadData()
    {
        drlProvince.DataSource = province.Province_GetByAll();
        drlProvince.DataTextField = "Name";
        drlProvince.DataValueField = "Id";
        drlProvince.DataBind();
        drlPayMethod.DataSource = paymethod.PayMethod_GetByAll(1);
        drlPayMethod.DataTextField = "Name";
        drlPayMethod.DataValueField = "Id";
        drlPayMethod.DataBind();

        var cus = customer.Customers_GetByTop(0, "Id = '" + Session["UserName_ID"] + "'", "");
        lblUserName.Text = cus.Rows[0]["FullName"].ToString();
        lblUserMail.Text = cus.Rows[0]["Mail"].ToString();

        if (Session["GioHang"] == null)
        {
            Response.Redirect("~/Trangchu.aspx");
        }
        else
        {
            tbGioHang = (DataTable)Session["GioHang"];
            if (tbGioHang.Rows.Count > 0)
            {
                string strnumber = string.Format("{0:0,0}", float.Parse(tbGioHang.Compute("Sum(TongTien)", "").ToString()));

                ltrTongTien.Text = strnumber;
                lblTongTien.Text = dinhdangtien((float.Parse(strnumber) + 60000).ToString());
                gvGiohang.DataSource = tbGioHang;
                gvGiohang.DataBind();
            }
            else
            {
                ltrThongbao.Text = "<h3>Giỏ hàng của bạn hiện không có sản phẩm nào</h3>";
                btnDatHang.Visible = false;
                lblTongTien.Text = "60,000";
            }

        }
    }
    public string dinhdangtien(string money)
    {
        return string.Format("{0:0,0}", float.Parse(money));
    }
    protected void btnDatHang_Click(object sender, EventArgs e)
    {
        BUS_Order order = new BUS_Order();
        BUS_OrderDetail orderdetail = new BUS_OrderDetail();
        var dt = order.Order_GetByTop(1, "", "Id DESC");
        int Order_Id = 0;
        if (dt.Rows.Count > 0)
        {
            Order_Id = int.Parse(dt.Rows[0]["Id"].ToString()) + 1;
        }
        else
        {
            Order_Id = 1;
        }
        DTO_Order objOrder = new DTO_Order();
        objOrder.ID = Order_Id;
        objOrder.Cus_Id = int.Parse(Session["UserName_Id"].ToString());
        
        objOrder.TotalMoney = float.Parse(tbGioHang.Compute("Sum(TongTien)", "").ToString()) + 60000;

        objOrder.PayMethod = int.Parse(drlPayMethod.SelectedValue.ToString());
        objOrder.Date = string.Format("{0:G}",DateTime.Now);
        objOrder.Cus_Name = txtCus_Name.Text;
        objOrder.Cus_Add = txtCus_Add.Text;
        objOrder.Cus_Province = int.Parse(drlProvince.SelectedValue.ToString());
        objOrder.Cus_Phone = txtCus_Phone.Text;
        objOrder.Check = false;
        objOrder.Finish = false;
        order.Order_Insert(objOrder);

        DTO_OrderDetail objOrdeDetail = new DTO_OrderDetail();
        foreach (DataRow row in tbGioHang.Rows)
        {

            objOrdeDetail.Order_Id = Order_Id;
            objOrdeDetail.Product_Id = int.Parse(row[0].ToString());
            objOrdeDetail.Quantily = int.Parse(row[3].ToString());
            orderdetail.OrderDetail_Insert(objOrdeDetail);
            product.Product_Update(row[0].ToString(), "[Buy] = [Buy]+" + row[3].ToString());
        }

        //Response.Write(objOrder.Cus_Name);

        //Response.Redirect("Trangchu.aspx");

        Literal1.Text = "<script>alert('Cám ơn bạn đã mua hàng. Chúng tôi  sẽ liên hệ vơi bạn ngay.!');location='Trangchu.aspx'</script>";
        Session["GioHang"] = null;
    }
}